<template>
	<view class="wx-createGroup" :style="'height: calc(100vh - ' + windowTop + 'px)'">
		<watermark></watermark>
		<view class="wx-createGroup-check"><uni-indexed-list-wx showSelect :showAdd="false" :options="list" @click="bindClick"></uni-indexed-list-wx></view>
		<view style="height: 60px;"></view>
		<view class="wx-createGroup-foot">
			<view class="wx-btn" @click="createGroup" >
				完成
				<text v-if="select.length > 0">({{ select.length }})</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [],
			select: [],
			windowTop: 0,
            is_submit:0,
			groupId: ''
		};
	},
	onLoad(e) {
		this.groupId = e.groupId;
	},
	mounted() {
		this.getflist();
		uni.getSystemInfo({
			success: res => {
				this.windowTop = res.windowTop;
			}
		});
	},
	methods: {
		createGroup() {
			if(this.select.length<=0){
				uni.showToast({
					title:'未选择',
					icon:'none'
				})
				return
			}
			var ids = [];
			for (var i = 0; i < this.select.length; i++) {
				//if(this.select[i].data.addtype=='N'){
					ids.push(this.select[i].data.userId);
				//}
			}
			uni.showLoading({
				title:'提交中'
			})
			var formData = {
				groupId: this.groupId,
				list: ids
			};
            if(this.is_submit==1)return;this.is_submit=1;
			this.$http.request({
				url: '/group/invitationGroup',
				method: 'POST',
				data: JSON.stringify(formData),
				success: res => {
					if (res.data.code == 200) {
						var show_msg = res.data.msg;
						if(res.data.data){
							var content = '你邀请'+res.data.data.content+'加入了群聊';
                            if(res.data.data.portrait){
                                var key  = "GROUP"+this.groupId;
    							var data=this.$store.state.chatlist && this.$store.state.chatlist[key]?this.$store.state.chatlist[key]:{};
    							
    							data.portrait=res.data.data.portrait
    							this.$store.dispatch('updateChatListInfoById', {
    								userId: key,
    								data: data
    							});
                            }
							this.$fc.pushAlertMsg({
    							msgContent: content, 	//msg内容
    							windowType: 'GROUP', 	//聊天室类型 SINGLE GROUP
    							userId: this.groupId, 	//ID
							});
						}
                        setTimeout(()=>{
                            this.is_submit=0;
                        },1000);
                        this.$fc.ToastBack(show_msg);
					}else{
					   this.is_submit=0;
					}
				}
			});
		},
		bindClick(e) {
			this.select = e.select;
		},
		getflist() {
			this.$http.request({
				url: '/friend/friendList',
				method: 'POST',
				data: JSON.stringify({
					groupId: this.groupId,
					is_not_group: 1
				}),
				success: res => {
					if (res.data.code == 200) {
						var data = res.data.data.data;
						var list = [];
						for (var i = 0; i < data.length; i++) {
							var item = data[i];
							if (item.userType == 'normal') {
								list.push({
									name: item.nickname,
									avatar: item.portrait,
									userId: item.fid,
									chatNo: item.chatNo
								});
							}
						}
						this.list = this.$fc.sortList({ list: list, key: 'name' });
					}
				}
			});
		}
	}
};
</script>

<style scoped>
.wx-createGroup {
	display: flex;
	flex-direction: column;
}
.wx-createGroup-check {
	height: 90%;
	position: relative;
}
.wx-createGroup-foot {
	width: 100%;
	height: 60px;
	line-height: 60px;
	background-color: #eee;
	position: fixed;
	bottom: 0;
	left: 0;
	text-align: right;
}
.wx-btn {
	height: 40px;
	line-height: 40px;
	min-width: 200rpx;
	text-align: center;
	border-radius: 12rpx;
	background-color: #05c160;
	color: #fff;
	font-size: 32rpx;
	display: inline-block;
	margin-top: 10px;
	margin-right: 20rpx;
}
</style>
